/**
 * Created by BINGO on 2016/10/28.
 */
var handleJstreeDefault = function() {
    $('#jstree-default').jstree({
        "core": {
            "themes": {
                "responsive": false
            }
        },
        "types": {
            "default": {
                "icon": "fa fa-folder text-warning fa-lg"
            },
            "file": {
                "icon": "fa fa-file text-inverse fa-lg"
            }
        },
        "plugins": ["types"]
    });

    $('#jstree-default').on('select_node.jstree', function(e,data) {
        var link = $('#' + data.selected).find('a');
        if (link.attr("href") != "#" && link.attr("href") != "javascript:;" && link.attr("href") != "") {
            if (link.attr("target") == "_blank") {
                link.attr("href").target = "_blank";
            }
            document.location.href = link.attr("href");
            return false;
        }
    });
};

//用户列表
var handleMenBerlist = function() {
    "use strict";
    if ($('#data-table').length !== 0) {
        $('#data-table').DataTable({
            language: {
                url: '/assets/plugins/DataTables/Chinese.json'
            },
            responsive: true,
            lengthMenu: [
                [5, 10, 15, 20, -1],
                [5, 10, 15, 20, "所有"] // change per page values here
            ],
            // set the initial value
            pageLength: 10,
            ajax:{
                "url":"/sysUser/datatable"
            },
            columns:[{
                data: null,
                title: "<input type='checkbox' name='checklist' id='checkall' />"
            },{
                data:"email",
                title:"登陆账号"
            },{
                data:"nickname",
                "title":"昵称"
            },{
                data:"gender",
                title:"性别",
                render: function (data, type, row, meta) {
                    if(data===0){
                        return '女'
                    }else if (data===1){
                        return '男'
                    }else
                        return ''
                }
            },{
                data:"organizationId",
                title:"所属组织"
            },{
                data:"status",
                title:"用户状态",
                render:function (data) {
                    return data==1?'<span class="label label-danger">禁用</span>':'<span class="label label-success">正常</span>'
                }
            },{
                data:"lastLoginTime",
                title:"最后登陆时间"
            },{
                data:"createTime",
                title:"创建时间"
            }],
            columnDefs: [{
                //   指定第一列，从0开始，0表示第一列，1表示第二列……
                targets: 0,
                sortable: false,
                render: function(data, type, row, meta) {
                    return '<input type="checkbox" name="checklist" value="' + row.id + '" />'
                }
            }],
            ordering: true,
            order: [1, "asc"],
            dom: "<'row' <'col-md-12'B>><'row'<'col-md-6 col-sm-12'l><'col-md-6 col-sm-12'f>r><'table-scrollable't><'row'<'col-md-5 col-sm-12'i><'col-md-7 col-sm-12'p>>"
        });
    }

};

var doSearch= function () {
    $('#data-table').DataTable().ajax.reload();
}

var SysUser = function () {
    "use strict";
    return {
        //main function
        init: function () {
            $.getScript('/assets/plugins/DataTables/js/jquery.dataTables.min.js').done(function() {
                handleMenBerlist();
            });
            handleJstreeDefault();
        }
    };
}();
